from DateTime import DateTime
from StringIO import StringIO

from Products.CMFCore.Expression import Expression
from Products.CMFCore.ActionInformation import ActionInformation
from Products.Archetypes.public import listTypes
from Products.Archetypes.Extensions.utils import installTypes, install_subskin

from Products.CoyMonSkin.config import *

def install(self):
    out = StringIO()

    installTypes(self, out, listTypes(PROJECTNAME), PROJECTNAME)
    install_subskin(self, out)

    props = self.portal_properties.site_properties
    tabs = getattr(props, 'use_folder_tabs')
    contents = getattr(props, 'use_folder_contents')

    for action_data in PORTAL_ACTIONS:
        if action_data[0] not in tabs:
            newtabs = list(tabs)
            newtabs.append(action_data[0])
            props._updateProperty('use_folder_tabs', newtabs)
        if action_data[0] not in contents:
            newcontents = list(contents)
            newcontents.append(action_data[0])
            props._updateProperty('use_folder_contents', newcontents)

    ###########################
    # actions section
    ###########################
    at = self.portal_actions
    actions = at._cloneActions()
    tabs_to_hide = []
    props = self.portal_properties.site_properties

    # change the default tabs
    for action in actions:
        if action.category == 'portal_tabs' and action.getId().lower() != 'home':
            tabs_to_hide.append(action.title)
            action.visible = 0

    # add the custom actions for portal tabs
    for action_data in PORTAL_ACTIONS:
        if action_data[4]:
            action = ActionInformation(
                id=action_data[2],
                title=action_data[1],
                action=Expression(text=action_data[3]),
                category=action_data[4],
                visible=1
            )
            actions.append(action)

    at._actions = tuple(actions)

    ###########################
    # site properties section
    ###########################
    if not hasattr(props, HIDDEN_ACTIONS):
        props._setProperty(HIDDEN_ACTIONS, tabs_to_hide, 'lines')

    for site_prop in SITE_PROPERTIES:
        props._setProperty(site_prop[0], site_prop[1], site_prop[2])

    ###########################
    # content section
    ###########################

    # create default folders, if they don't already exist
    for folder in PORTAL_ACTIONS:
        if folder[0]:
            self.invokeFactory(type_name=folder[0], id=folder[2], title=folder[1])

    # Modify the left and right slots of the portal
    right_slots = list(self.getProperty('right_slots'))
    left_slots  = list(self.getProperty('left_slots'))
    if not hasattr(props, HIDDEN_LEFT_SLOTS):
        props._setProperty(HIDDEN_LEFT_SLOTS, left_slots, 'lines')
    if not hasattr(props, HIDDEN_RIGHT_SLOTS):
        props._setProperty(HIDDEN_RIGHT_SLOTS, right_slots, 'lines')
    for slot in right_slots:
        if slot.find('portlet_calendar') == -1:
            left_slots.append(slot)
    self._updateProperty('left_slots',left_slots)
    self._updateProperty('right_slots','')

    print >> out, "Successfully installed %s." % PROJECTNAME
    return out.getvalue()

def uninstall(self):

    out = StringIO()
    
    # Change the slots as they use to be before CoyMon install
    #swap the hidden slots under portal_properties/site_properties
    props = self.portal_properties.site_properties
    right_slots = list(getattr(props, HIDDEN_RIGHT_SLOTS))
    left_slots  = list(getattr(props, HIDDEN_LEFT_SLOTS))
    self._updateProperty('left_slots',left_slots)
    self._updateProperty('right_slots',right_slots)

    # change the actions back to default
    at = self.portal_actions
    actions = at._cloneActions()

    hidden_tabs = []
    props = self.portal_properties.site_properties

    hidden_tabs = getattr(props, HIDDEN_ACTIONS)
    for action in actions:
        if action.title in hidden_tabs:
            action.visible = 1
    at._actions = tuple(actions)

    # delete the custom hidden properties created by CoyMon
    props._delProperty(HIDDEN_RIGHT_SLOTS)
    props._delProperty(HIDDEN_LEFT_SLOTS)
    props._delProperty(HIDDEN_ACTIONS)

    # delete more properties created by CoyMon
    for site_prop in SITE_PROPERTIES:
        props._delProperty(site_prop[0])

    print >> out, "Sucessfully uninstalled %s." % PROJECTNAME
    return out.getvalue()

